// Copyright (C) 2009 Jesse Jones
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

using System;

internal abstract class Shape
{
}

internal sealed class Square : Shape
{
	[Intersects4.Overload]
	public bool Intersects(Square rhs)
	{
		return true;
	}
}

internal sealed class TooFewArgs1
{
	[Intersects4.Overload]
	public static bool Intersects(Square lhs)
	{
		return true;
	}
}

internal sealed class TooFewArgs2 : Shape
{
	[Intersects4.Overload]
	public bool Intersects()
	{
		return true;
	}
}

internal sealed class TooManyArgs1
{
	[Intersects4.Overload]
	public static bool Intersects(Square lhs, Square rhs, Square hmm)
	{
		return true;
	}
}

internal sealed class TooManyArgs2 : Shape
{
	[Intersects4.Overload]
	public bool Intersects(Square rhs, Square hmm)
	{
		return true;
	}
}

internal sealed class BadReturn
{
	[Intersects4.Overload]
	public static int Intersects(Square lhs, Square rhs)
	{
		return 0;
	}
}

internal sealed class BadArg
{
	[Intersects4.Overload]
	public static bool Intersects(Square lhs, object rhs)
	{
		return false;
	}
}

internal sealed class Good
{
	[Intersects4.Overload]
	public static bool Intersects(Square lhs, Square rhs)
	{
		return false;
	}
}

internal sealed class Duplicate
{
	[Intersects4.Overload]
	public static bool Intersects(Square lhs, Square rhs)
	{
		return false;
	}
}

internal static class Program
{
	public static void Main(string[] args)
	{
		DoCheck(typeof(TooFewArgs1), "TooFewArgs1.Intersects has arity 1 but should have arity 2.");
		DoCheck(typeof(TooFewArgs2), "TooFewArgs2.Intersects has arity 0 but should have arity 1.");
		DoCheck(typeof(TooManyArgs1), "TooManyArgs1.Intersects has arity 3 but should have arity 2.");
		DoCheck(typeof(TooManyArgs2), "TooManyArgs2.Intersects has arity 2 but should have arity 1.");
		DoCheck(typeof(BadReturn), "BadReturn.Intersects has return type System.Int32 but should have bool (or a derived class).");
		DoCheck(typeof(BadArg), "BadArg.Intersects argument rhs is not compatible with type Shape.");
		Intersects4.Register(typeof(Good));
		DoCheck(typeof(Duplicate), "There is already an overload registered with the same signature as Duplicate.Intersects.");
	}
	
	private static void DoCheck(Type type, string expected)
	{
		try
		{
			Intersects4.Register(type);
			throw new Exception("Expected an exception for " + type);
		}
		catch (InvalidOperationException e)
		{
			if (e.Message != expected)
				throw new Exception(string.Format("Expected '{0}', but got '{1}'.", expected, e.Message));
		}
	}
}
